class ListCtrl < Wx::ListCtrl
attr_accessor :indices,:array,:nodes,:root_nodes,:nodepos,:item,:selItem
     def initialize(parent,style,gui,type='transfer')
       
       super(parent,:style=>style)
        @il = Wx::ImageList.new(16,16)
        bmp_file = "expand-treeitem.png"
        
        @idx1 = @il.add(Wx::Bitmap.new(bmp_file))
        bmp_file = "de-expand_treeitem.png"
        
        @idx2 = @il.add(Wx::Bitmap.new(bmp_file))
        bmp_file = "empty-treeitem.png"
        
        @idx3 = @il.add(Wx::Bitmap.new(bmp_file))
        set_image_list(@il, Wx::IMAGE_LIST_SMALL)
       @root_nodes=[]
       @gui = gui
       @type = type
       @indices = []
       @array = []
       @nodes = {}
       @nodepos = []

#         evt_list_item_deselected(get_id()) {|event| on_item_deselected(event)}
     end
  def hide_node node
n = node
if n.node_type == NODE_TYPE_EXPANDITEM
             n.unexpand n
             n.expanded = 0
           end
p "============ tree =============="
    i = get_node_index(n.get_index)
p "ty#{i}"
begin
if i!=nil
    @array.delete_at i
p @indices.length
    @indices.delete_at i
p @indices.length
p i
end
rescue
end
p "+++++++++++++++++++++++++"
# exit
#     @array = []
#     @indices.each {|path|
#       @array << get_node(path).iter
#     }
#     @array << ""
    refresh
    update
  end
  def add_static_ctrl ctrl,*args
    wi = self.parent
    staticCtrl = ctrl::new(*args)
    staticCtrl.hide
    @staticCtrls << staticCtrl
  end
  def show_static_ctrl item,col,index
    ctrl = @staticCtrls[index]
#     item,col
#     ctrl.set_x = x
#     ctrl.set_y = y
  end
  def on_get_item_text(item, col)
# p "fsoto"
       begin
         node = get_node(get_node_path(item))
         ret = @array[item][col]
         if ret =~ /ListStaticCtrl ([0-9]+)/
           show_ctrl(item,col,get_static_ctrl($1.to_i))
         end
         ret = "" if ret == nil
         if node != nil
         ret = ("     "*node.depth) + ret if col == 0
         end
       rescue
         ret = "" if ret == nil
       end
       return ret
      
  end
  def append_array(arr)
    @array << arr
  end
  def set_indices(arr)
    @indices = arr
  end
  def add_node(node)
    node.index = [@root_nodes.length]

    iter = node.iter
    array=iter
    structure(node,@indices,@array)
    set_indices(@indices)
    @root_nodes << node
    refresh
  end
  def structure(node,indices,larr,offset = 0)
    if node.node_type == NODE_TYPE_EXPANDITEM
      root = node.parent.index
      larr << node.iter
      indices << node.index
      @nodes[node]={}
      @nodes[node]['index'] = indices.last
      @nodepos << node
      node.nodepos = @nodepos.length-1

# p 1
      node.children.each_with_index {|child,index|
#         p child.parent.children
        child.set_index
        if child.node_type == NODE_TYPE_EXPANDITEM
          structure(child,indices,larr,index)
        else
            larr << child.iter
            indices << child.index
            @nodes[child]={}
            @nodes[child]['index'] = indices.last
            @nodepos << child
            child.nodepos = @nodepos.length-1
        end
      }
    else
       node.iter.each {|item|
         larr << item
         indices node.index
         @nodes[node]={}
         @nodes[node]['index'] = indices.last
         @nodepos << node
         node.nodepos = @nodepos.length-1
       }
    end
    @nodepos << node
    node.nodepos = @nodepos.length-1
    return indices,larr
  end
  def get_node_index(path=[0,0,0])
    @indices.index(path)
  end
  def get_node_path(index=0)
    return @indices[index]
  end
  def get_node(path)
    begin
    @nodes.find {|n| n[0].get_index == path}[0]
    rescue
     nil
    end
  end
  def on_item_activated(event)
        
        @selItem = event.get_index()
        @item = event.get_item()
        get_column(1,@item)
        path = get_node_path(@selItem)
        p "selected index #{path.join(":")}"
        node = get_node(path)
        if node.node_type == NODE_TYPE_EXPANDITEM
p node.iter
if node.get_index.join(":") == "0:0:0"
# exitD
end
          if node.expanded == 1
            node.unexpand node
            node.expanded = 0
            p 'foo'
          else
            node.expanded = 1
            node.reexpand node,@selItem
          end
          refresh
        end
  end


  def on_item_selected(event)
        
        @selItem = event.get_index()
        @item = event.get_item()
        get_column(1,@item)
        index = @indices[@selItem]
#         p "selected index #{index.join(":")}"
        begin
          if defined?(@tarr) and @tarr[@selItem] != nil
# p @tarr[@selItem]
# exit
            @transfer = @tarr[@selItem]
            @gui.htmlproc[@type].call(@selItem,@transfer)
          elsif @type=="search"
            @gui.htmlproc[@type].call(@selItem)
          end
        end
#         item = @item
#         titem = @currentItem
#         length(titem)
#         @log.write_text('on_item_selected: "%s", "%s", "%s", "%s"' % [@currentItem, get_item_text(@currentItem), 
#                             @item.get_text(), get_column(2,@item) ? @item.get_text() : nil])
    end
    
    def on_get_item_column_image(item, col)
      begin
        if col == 0
          node = get_node(get_node_path(item))
          if node.node_type == NODE_TYPE_EXPANDITEM and node.expanded == 1 and node.get_children.length > 0
            return @idx2
          elsif node.node_type == NODE_TYPE_EXPANDITEM and node.get_children.length > 0
            return @idx1
          else
            return @idx3
          end
        else
          return -1
        end
      rescue
        return -1
      end
    end
    
    def on_get_item_attr(item)
      begin
        node = get_node(get_node_path(item))
        if @root_nodes.index(node) != nil
          return @attr1
        elsif node.iter[1] == ":AT"
          return @attr2    
        elsif node.iter[1] == ":AND"
          return @attr3
        elsif node.iter[1] == "LOCKS"
          return @attr4
        else
          return @attr0
        end  
      rescue
    
      end
    end
end